<template>
  <div>
    <!-- 面包屑导航区域-->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!--卡片试图区域-->
    <el-card>
      <!--搜索与添加区域-->
      <el-row :gutter="20">
        <el-col :span="10">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            size="small "
            @clear="getpaintingList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getpaintingList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="insertpainting"
        >新增</el-button>
        </el-col>
        
      </el-row>

      <!--用户列表区域-->
      <el-table :data="paintingList" border stripe>
        <el-table-column  label="ID" prop="id"></el-table-column>
        <el-table-column label="用户名" prop="userId"></el-table-column>
        <el-table-column label="图片" >
          <template slot-scope="scope">
            <el-image
             style="width: 100px; height: 100px"
            :src="scope.row.paintingUrl"
            :fit="fit"></el-image>
        </template>
        </el-table-column>
        <el-table-column label="时间" prop="createTime"></el-table-column>
        <el-table-column label="操作" width="200px">
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-button
              type="success"
              round="true"
              size="mini"
              @click="updatepainting(scope)"
            >编辑</el-button>
            <!-- 删除按钮 -->
            <el-button
              type="danger"
              round="true"
              size="mini"
              	
              @click="deletepainting(scope)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!--分页区域-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

 
    <el-dialog :visible.sync="dialogVisible" :title="dialogType==='new'?'新增':'编辑'" @close="closeDialog" >
      <el-form :model="painting" label-width="80px" label-position="left">
        <el-form-item label="用户ID">
          <el-input v-model="painting.userId" placeholder="用户ID"  />
        </el-form-item>
        <!-- <el-form-item label="时间">
          <el-input v-model="painting.createTime" placeholder="创建时间"  />
        </el-form-item> -->
        <el-form-item label="图片地址">
          <el-input v-model="painting.paintingUrl" placeholder="图片地址"/>
        </el-form-item>
      </el-form>
      <div style="text-align:right;">
        <el-button type="danger" @click="dialogVisible=false">关闭</el-button>
        <el-button type="primary" @click="confirmRole()">提交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listEmploysys,updateEmploysys,addEmploy } from '@/api/aipating/aihuihua'
const defaultpainting = {
  userId: '',
  paintingUrl: '',
 
}
export default {
  data() {
    return {
      // 获取用户列表的参数对象
      queryInfo: {
        query: '',
        // 当前的页数
        pagenum: 1,
        // 当前每页显示的条数
        pagesize: 5
      },
      paintingList: [],
      total: 0,
      painting: Object.assign({}, defaultpainting),
      dialogVisible: false,
      dialogType: '',
      checkStrictly: false,
      editForm: {},

      // 修改表单验证对象
    }
  },
  computed: {
    routesData() {
      return this.routes
    }
  },
  created() {
    this.getpaintingList()
  },
  methods: {
    async getpaintingList() {
      const   res  = await listEmploysys(this.queryInfo.query,this.queryInfo.pagenum,this.queryInfo.pagesize,"1")
      console.log("painting:"+res)
      this.paintingList = res.data.paintingList
      this.total = res.data.total
    },
    // 监听 pagesize 改变的事件
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getpaintingList()
      console.log(newSize)
    },
    // 监听页码值改变的事件
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getpaintingList()
      console.log(newPage)
    },
    // 修改用户状态
    async userStateChanged(userInfo) {
      const { data: res } = await this.$http.put(
        `users/${userInfo.id}/state/${userInfo.mg_state}`
      )
      if (res.meta.status !== 200) {
        userInfo.mg_state = !userInfo.mg_state
        return this.$message.error(res.meta.msg)
      }
      this.$message.success(res.meta.msg)
    },
    // 监听添加用户对话框的关闭事件
    async addDialogClosed() {
      // 重置表单
      this.$refs.addFormRef.resetFields()
    },
    // 修改权限
    async updatepainting(scope) {
      this.dialogVisible=true
      this.painting=scope.row
      this.dialogType='edit'
    },
    // 监听修改用户对话框的关闭事件
    editDialogClosed() {
      this.$refs.editFormRef.resetFields()
    },
    // 修改角色
    async deletepainting({ $index, row }) {
        this.painting=row
        this.painting.isDelete='0'
      this.$confirm('确定是否要删除改角色?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '关闭',
        type: 'warning'
      })
        .then(async() => {
          await updateEmploysys(this.painting)
          this.paintingList.splice($index, 1)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(err => { console.error(err) })
    },
    async confirmRole() {
      const isEdit = this.dialogType === 'edit'
      if (isEdit) {
          await updateEmploysys(this.painting)
          for (let index = 0; index < this.paintingList.length; index++) {
          if (this.paintingList[index].id === this.painting.id) {
            this.paintingList.splice(index, 1, Object.assign({}, this.painting))
            break
          }
        }
      } else {
        console.log(this.painting)
        const { data } = await addEmploy(this.painting)
        this.painting.id = data
        this.paintingList.push(this.painting)
      }

      this.dialogVisible = false
      this.$notify({
        title: 'Success',
        dangerouslyUseHTMLString: true,
        message: `
            <div>修改成功</div>
          `,
        type: 'success'
      })
    },
    insertpainting(){
      
        this.dialogVisible=true
        // this.painting=[]
        this.dialogType='new'
    },
    closeDialog(){
        this.painting=[]
    }
  }
}
</script>

<style lang="less" scoped></style>

